(function($) {
    "use strict";
    $.extend($.fn, {
        selectPicker: function(options) {
            var options = $.extend({}, $.fn.selectPicker.defaults, options);
            return this.each(function() {
                var $this = $(this);
                var addon = $this.find(options.btnEl);
                var idInput = $this.find(options.valueEl);
                var valueInput = $this.find(options.textEl);
                var setPickerResult = ($.isFunction(options.setPickerResult))?options.setPickerResult:(function(results){
                    idInput.val(results[options.idField]);
                    valueInput.val(results[options.valueField]);
                });
                var picker =$this;
                if (!picker.attr('data-picker-url')) {
                    picker = $("[data-picker-url]", this);
                }
                var url =  picker.attr('data-picker-url');
                var width = picker.attr('data-picker-width');
                var height = picker.attr('data-picker-height');
                var title = picker.attr('data-picker-title');
                addon.click(function(e) {
                    var frameWin = null;
                    var dialog = bootboxExt.dialogFrame({
                        width: Number(width?width:options.width),
                        height: Number(height?height:options.height),
                        url: url,
                        title: title?title:options.title,
                        onEscape: options.onEscape,
                        show: true,
                        backdrop: true,
                        animate: true,
                        buttons: {
                            success: {
                                label: options.selectBtnLabel,
                                className: options.selectBtnClass,
                                callback: function() {
                                    if ($.isFunction(frameWin.doSelect)) {
                                        var results = frameWin.doSelect();
                                        setPickerResult(results);
                                        return true;
                                    }
                                    return false;
                                }
                            },
                            cancel: {
                                label: options.cancelBtnLabel,
                                className: options.cancelBtnClass,
                                callback: function() {
                                    return true;
                                }
                            }
                        }
                    });
                    dialog.on("shown.bs.modal", function(e) {

                    });
                    var frame = dialog.getFrame();
                    frame.load(function(e) {
                        frameWin = e.target.contentWindow;
                    });
                });
            });
        }
    });
    $.fn.selectPicker.defaults = {
        btnEl: '.pickerBtn',
        valueEl: ".input-value",
        textEl: ".input-text",
        idField: "id",
        valueField: "name",
        title: "请选择",
        width: 700,
        height: 500,
        onEscape: function() {},
        selectBtnClass: "btn-success",
        cancelBtnClass: "btn",
        selectBtnLabel: "选择",
        cancelBtnLabel: "取消",
        setPickerResult: null
    }
}(window.jQuery));